import java.lang.reflect.*;

class student{
    private String name = "ee";
    int age = 0;

    public student() {
        this.name = "hadage";
        this.age = 19;
    }
    private student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private void print() {
        System.out.println("halo");
    }

    private void print(String s) {
        System.out.println("halo " + s);
    }
    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class fanshe {

    public static void main(String[] args) {
        //反射的使用
//        createInstance();
//        reflectprivate();
//        reflectprivatefield();
        reflectprivateMethod();
    }

    private static void reflectprivateMethod() {
        //=反射私有方法
        try {
            Class<?> studentClass = Class.forName("student");
            Method method = studentClass.getDeclaredMethod("print");//有declared修饰的就是可以反射private修饰的方法
            method.setAccessible(true);

            student student = (student) studentClass.newInstance();
            method.invoke(student);//调用这个类对应的方法

            Method method1 = studentClass.getDeclaredMethod("print", String.class);
            method1.setAccessible(true);

            student student1 = (student) studentClass.newInstance();
            method1.invoke(student1,"abababa");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

    }

    private static void reflectprivatefield() {
        //获得private修饰的属性 字段
        try {
            Class<?> studentClass = Class.forName("student");
            Field field = studentClass.getDeclaredField("name");

            field.setAccessible(true);

            student student = (student) studentClass.newInstance();

            field.set(student,"azhe");//修改
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }

    }

    private static void reflectprivate() {
        //通过CLass调用private修饰的方法
        try {
            Class<?> studentClass = Class.forName("student");

            Constructor<student> constructor = (Constructor<student>) studentClass.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);//需要调用private修饰的方法都要确认

            student student = constructor.newInstance("hhh", 19);

            System.out.println(student);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    private static void createInstance() {
        try {
            Class<?> studentClass = Class.forName("student");
//            通过一个class对象去创建一个对象
            student student = (student) studentClass.newInstance();//这样就创建了一个student对象，且并没有是用new
            System.out.println(student);


        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }

    public static void main1(String[] args) throws ClassNotFoundException {
        //获得一个class对象 class对象只有一个
        Class<?> studentClass = Class.forName("student");

        Class<student> studentClass1 = (Class<student>) Class.forName("student");

        student student1 = new student();
        Class<?> studentClass2 = student1.getClass();

        Class<?> studentClass3 = student.class;
    }
}
